package com.ljh.aiplatform.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ljh.aiplatform.model.dto.question.QuestionContentDTO;
import com.ljh.aiplatform.model.dto.question.QuestionContentOption;
import com.ljh.aiplatform.model.entity.App;
import com.ljh.aiplatform.model.entity.Question;
import com.ljh.aiplatform.model.entity.ScoringResult;
import com.ljh.aiplatform.model.entity.UserAnswer;
import com.ljh.aiplatform.service.QuestionService;
import com.ljh.aiplatform.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * 自定义得分策略
 */
@ScoringStrategyConfig(appType = 0, scoringStrategy = 0)
public class CustomScoreScoringStrategy implements ScoringStrategy {

    @Resource
    private QuestionService questionService;

    @Resource
    private ScoringResultService scoringResultService;

    @Override
    public UserAnswer doScoring(List<String> choices, App app) {
        //1: 根据appid获取题目（question）和题目的信息（scoring_result)
        Long appId = app.getId();
        LambdaQueryWrapper<Question> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Question::getAppId, appId);
        Question question = questionService.getOne(lambdaQueryWrapper1);
        LambdaQueryWrapper<ScoringResult> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(ScoringResult::getAppId, appId);
        List<ScoringResult> scoringResultList = scoringResultService
                .list(lambdaQueryWrapper2).stream()
                .sorted(Comparator.comparing(ScoringResult::getResultScoreRange).reversed()).toList();
        //2: 统计分数
        //2.1：取出题目列表
        String questionContent = question.getQuestionContent();
        List<QuestionContentDTO> questionContentDTOS = JSONUtil.toList(questionContent, QuestionContentDTO.class);
        //2.2：获取总分
        int sum = 0;
        for (int i = 0; i < questionContentDTOS.size(); i++) {
            final List<QuestionContentOption> options = questionContentDTOS.get(i).getOptions();//获取选项列表
            for (QuestionContentOption option : options) {
                if (choices.get(i).equals(option.getKey())) {
                    sum += Optional.of(option.getScore()).orElse(0);
                }
            }
        }
        System.out.println(sum);
        //3：遍历每种评分结果
        ScoringResult maxScoringResult = scoringResultList.get(0);//设置一个初始值
        for (ScoringResult scoringResult : scoringResultList) {
            if (sum >= scoringResult.getResultScoreRange()) {
                maxScoringResult = scoringResult;
                break;
            }
        }
        //4：填充属性返回
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        userAnswer.setResultScore(sum);
        return userAnswer;
    }
}
